package com.ccb.testcenter.serviceMapingFactory.impl;

import java.util.LinkedList;
import java.util.List;

import com.ccb.testcenter.config.entity.FilterConfig;
import com.ccb.testcenter.config.entity.MappingConfig;
import com.ccb.testcenter.config.entity.RequestConfig;
import com.ccb.testcenter.config.entity.ResponseConfig;
import com.ccb.testcenter.configuration.Configuration;
import com.ccb.testcenter.servicemaping.IFilter;
import com.ccb.testcenter.servicemaping.Mapping;
import com.ccb.testcenter.servicemaping.Request;
import com.ccb.testcenter.servicemaping.Response;
import com.ccb.testcenter.servicemaping.Trcd;
import com.ccb.testcenter.serviceMapingFactory.MappingFactory;
 /**
  * 
   * 类名：DefaultMappingFactory
   * 创建人：li_dk
   * 修改人：li_dk
   * 创建时间：2017年1月8日 下午9:09:24
   * 修改时间：2017年1月8日 下午9:09:24
   * 文件版本：@version 1.0.0
   *
  */
public class DefaultMappingFactory  implements MappingFactory{
	protected final Configuration configuration;
	private DefaultRequestFactory defaultRequestFactory;
	private DefaultResponseFactory defaultResponseFactory;

	public DefaultMappingFactory(Configuration configuration) {
		super();
		this.configuration = configuration;
		defaultRequestFactory=new DefaultRequestFactory(configuration);
		defaultResponseFactory=new DefaultResponseFactory(configuration);
	}

	@Override
	public Mapping buildMapping(MappingConfig mappingConfig) {
		Mapping mapping = new Mapping();
		Trcd trcd = new Trcd();
		Request request = new Request();
		Response response = new Response();
		String sourceCode = mappingConfig.getSource();
		String distinationCode = mappingConfig.getName();
		trcd.setSourceCode(sourceCode);
		trcd.setDistinationCode(distinationCode);
		mapping.setTrcd(trcd);
		List<FilterConfig> filters = mappingConfig.getFilters();
		List<IFilter> iFilters = new LinkedList<IFilter>();
		  for (int i = 0; i < filters.size(); i++) {
			  IFilter filter = this.configuration.getIFilter(filters.get(i).getId());
			  iFilters.add(filter);
		}
		  mapping.setFilters(iFilters);
		RequestConfig requestConfig = mappingConfig.getRequestConfig();
		ResponseConfig responseConfig = mappingConfig.getResponseConfig();
		request = defaultRequestFactory.buildRequest(requestConfig);
		//调用ResponseConfig 转Response 方法
		response = defaultResponseFactory.buildResponse(responseConfig);
	    mapping.setRequest(request);
	    mapping.setResponse(response);
	    this.configuration.addMapping(trcd, mapping);
		return mapping;
	}

}
